/**
 * @Author: Mr.Yan
 * @date: 2019/1/9
 * @Description: 创建全局变量，查询
 */
var title = "",
    businessType = "",
    operName = "",
    befdate = "",
    enddate = "";
/**
 * 初始化
 */
$(function () {
    init();
    tableInit();
});

/**
 * 加载layui样式
 */
function init() {
    layui.use(['form', 'laydate', 'dtree'], function () {

        var form = layui.form
            , laydate = layui.laydate
            , dtree = layui.dtree
            , nowdate = getNowFormatDate();
        //年月日范围选择选择器
        laydate.render({
            elem: '#month'
            , max: 'nowdate'
            , range: '至'
            , done: function (value) {
                if (value != null && (value.indexOf("至") != -1)) {
                    var date = value.split("至");
                    //  开始日期
                    befdate = date[0].trim();
                    //   结束日期
                    enddate = date[1].trim();
                } else {
                    befdate = "";
                    enddate = "";
                }
            }
        });

    });
}

// 数据展示
function tableInit() {
    layui.use(['form', 'table'], function () {
        var loading = window.top.layer.load(0, {shade: false});

        var form = layui.form
            , table = layui.table;

        //展示已知数据
        table.render({
            elem: '#tb'
            , url: prefixOperLog + "/list"
            , toolbar: '#toolbar'
            , cellMinWidth: 200
            , where: {
                //  设置参数
                "params": JSON.stringify({
                    "title": title,
                    "businessType": businessType,
                    "operName": operName,
                    "befdate": befdate,
                    "enddate": enddate,
                })
            }
            , cols:
                [[
                    {checkbox: true,fixed: 'true'}
                    , {type: 'numbers', title: '序号',fixed: 'true'}
                    , {field: 'title', title: '操作模块',fixed: 'true'}
                    , {
                        field: 'businessType', title: '操作类型',fixed: 'true',
                        templet: function (d) {
                            switch (d.businessType) {
                                case 1 :
                                    return "<button class=\"layui-btn layui-btn-sm\">新增</button>";
                                case 2 :
                                    return "<button class=\"layui-btn layui-btn-sm layui-btn-normal\">修改</button>";
                                case 3 :
                                    return "<button class=\"layui-btn layui-btn-sm layui-btn-warm\">删除</button>";
                                case 4 :
                                    return "<button class=\"layui-btn layui-btn-sm \">授权</button>";
                                case 5 :
                                    return "<button class=\"layui-btn layui-btn-sm layui-btn-normal\">导出</button>";
                                case 6 :
                                    return "<button class=\"layui-btn layui-btn-sm layui-btn-warm\">导入</button>";
                                case 7 :
                                    return "<button class=\"layui-btn layui-btn-sm \">强退</button>";
                                case 8 :
                                    return "<button class=\"layui-btn layui-btn-sm layui-btn-normal\">生成代码</button>";
                                case 9 :
                                    return "<button class=\"layui-btn layui-btn-sm layui-btn-warm\">清空数据</button>";
                                case 10 :
                                    return "<button class=\"layui-btn layui-btn-sm \">重置密码</button>";
                                default:
                                    return "<button class=\"layui-btn layui-btn-sm layui-btn-danger\">其他</button>";
                            }

                        }
                    }
                    , {field: 'operName', title: '操作人员'}
                    , {field: 'operIp', title: '主机'}
                    , {field: 'operLocation', title: '操作地点'}
                    , {
                        field: 'status', title: '操作状态',
                        templet: function (d) {
                            switch (d.status) {
                                case "0" :
                                    return "<button class=\"layui-btn layui-btn-sm\">成功</button>";
                                case "1" :
                                    return "<button class=\"layui-btn layui-btn-sm layui-btn-warm\">失败</button>";
                                default:
                                    return "<button class=\"layui-btn layui-btn-sm layui-btn-danger\">未知</button>";
                            }

                        }
                    }
                    , {field: 'operTime', title: '操作时间'}
                    , {fixed: 'right', width: 240, align: 'center', toolbar: '#tool'}
                ]]
            , page:
                true //是否显示分页
            , limits:
                [10, 20, 30]
            , limit:
                10 //每页默认显示的数量
            , done:
                function () {
                    layTool.toplayer.close(loading);
                }
        });

        //头工具栏事件
        table.on('toolbar(tb)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;
            switch (obj.event) {
                case 'remove':
                    remove(data);
                    break;
                case 'removeAll':
                    removeAll(data);
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(tb)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            switch (obj.event) {
                case 'detail':
                    detail(data);
                    break;
            }
        });

    })
}

/**
 * @Description: 删除所选数据
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/27
 */
function remove(data) {
    if (!isEmpty(data)) {
        var dataLength = data.length;

        if (dataLength > 0) {
            layTool.showConfirm("确定要删除您选择的" + dataLength + "个操作日志信息吗？", function () {
                var loading = "";
                var operIds = "";
                for (var i = 0; i < dataLength; i++) {
                    operIds += data[i].operId + ",";
                }
                $.ajax({
                    url: prefixOperLog + "/delete",
                    data: {
                        operIds: operIds.substring(0, operIds.length - 1)
                    },
                    beforeSend: function () {
                        loading = layTool.toplayer.load(0, {shade: false});
                    },
                    success: function (data) {
                        layTool.toplayer.close(loading);
                        if (!isEmpty(data)) {
                            var errcode = data.errcode;
                            var errmsg = data.errmsg;
                            if (errcode == "0") {
                                tableInit();
                                layTool.showSucAlert(errmsg);
                            } else {
                                layTool.showAlert(errmsg);
                            }
                        }
                    },
                    error: function () {
                        layTool.toplayer.close(loading);
                        AjaxErro();
                    }
                })

            });
        } else {
            layTool.showMsg("只能同时选择1个或多个操作日志信息吗，您选择了" + dataLength + "个");
        }
    } else {
        layTool.showMsg("请选择要删除的操作日志信息吗");
    }
}


/**
 * @Description: 清空登录入职
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/27
 */
function removeAll() {
    layTool.showConfirm("确定要清空操作日志信息吗？", function () {
        var loading = "";
        $.ajax({
            url: prefixOperLog + "/deleteAll",
            beforeSend: function () {
                loading = layTool.toplayer.load(0, {shade: false});
            },
            success: function (data) {
                layTool.toplayer.close(loading);
                if (!isEmpty(data)) {
                    var errcode = data.errcode;
                    var errmsg = data.errmsg;
                    if (errcode == "0") {
                        tableInit();
                        layTool.showSucAlert(errmsg);
                    } else {
                        layTool.showAlert(errmsg);
                    }
                }
            },
            error: function () {
                layTool.toplayer.close(loading);
                AjaxErro();
            }
        })

    });
}

/**
 * @Description: 查看详情
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/27
 */

function detail(data) {
    var operId = data.operId;
    var title = "查看操作日志信息",
        url = prefixOperLog + "/openOperLogDetail?operId=" + operId,
        width = "40%",
        height = "80%";
    //  打开新窗口
    layTool.openDialog(title, url, width, height);
}
/**
 * @Description:    查询事件
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/7
 */

function serach() {
    title = $("#title").val();
    businessType = $("#businessType").val();
    operName = $("#operName").val();
    tableInit();
}